/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.moedik.model;

import com.moedik.classes.Payment;
import java.security.SecureRandom;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sammy
 */
public class PaymentModel extends Model {
        
    public PaymentModel() {
        super();
    }

    public ArrayList<Payment> getAll() {
        ArrayList<Payment> payments = new ArrayList<Payment>();
        Statement stmt = null;

        try {
            stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT `payments`.`id` AS `id`, `payments`.`billno` AS `billno`, `payments`.`booking_code` AS `booking_code`, `payments`.`status` AS `status` FROM `payments` WHERE `payments`.`status` < 2");

            while (rs.next()) {
                Payment payment = new Payment(rs.getInt("id"), rs.getString("billno"), rs.getString("booking_code"), rs.getInt("status"));
                payments.add(payment);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ConfigModel.class.getName()).log(Level.SEVERE, null, ex);
        }

        return payments;
    }

    public String register(int userId, int serviceType, String date, String bookingCode) {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        int key = 0;

        String chars = "0123456789";
        String chars2 = "123456789";
        char[] buf = new char[16];
        SecureRandom r = new SecureRandom();

        buf[0] = chars2.charAt(r.nextInt(chars2.length()));

        for (int i = 1; i < buf.length; i++) {
            buf[i] = chars.charAt(r.nextInt(chars.length()));
        }

        String billno = new String(buf);

        try {
            String query = "INSERT INTO payments (id, user_id, service, date, billno, booking_code, status) VALUES (NULL, '" + userId + "', '" + serviceType + "', '" + date + "', '" + billno + "', '" + bookingCode + "', '0')";
            stmt = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
            stmt.executeUpdate();
            rs = stmt.getGeneratedKeys();

            rs.next();
            key = rs.getInt(1);
        } catch (SQLException ex) {
            Logger.getLogger(PaymentModel.class.getName()).log(Level.SEVERE, null, ex);
        }

        return billno;
    }

    public String confirm(String billno) {
        String result = null;
        int rowCount = 0;

        try {
            PreparedStatement stmt = null;

            Payment payment = this.get(billno);

            if (payment != null) {
                if (payment.getStatus() == 2) {
                    result = payment.getBookingCode();
                } else {
                    String query = "UPDATE payments SET status = '1' WHERE billno = '" + billno + "'";
                    stmt = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
                    rowCount = stmt.executeUpdate();
                    this.lm.confirmBook(billno);
                    result = "PENDING";
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(PaymentModel.class.getName()).log(Level.SEVERE, null, ex);
        }

        return result;
    }

    public int approve(int id) {
        int rowCount = 0;

        try {
            PreparedStatement stmt = null;

            String query = "UPDATE payments SET status = '2' WHERE id = '" + id + "'";
            stmt = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
            rowCount = stmt.executeUpdate();
            this.lm.approveBook(id);
        } catch (SQLException ex) {
            Logger.getLogger(PaymentModel.class.getName()).log(Level.SEVERE, null, ex);
        }

        return rowCount;
    }

    public int delete(int id) {
        int rowCount = 0;

        try {
            PreparedStatement stmt = null;

            String query = "DELETE FROM payments WHERE id = '" + id + "'";
            stmt = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
            rowCount = stmt.executeUpdate();
            this.lm.log(id+" telah dihapus dari table payment");
        } catch (SQLException ex) {
            Logger.getLogger(PaymentModel.class.getName()).log(Level.SEVERE, null, ex);
        }

        return rowCount;
    }

    public Payment get(String billno) {
        Payment payment = null;
        Statement stmt = null;

        try {
            stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT `payments`.`id` AS `id`, `payments`.`billno` AS `billno`, `payments`.`booking_code` AS `booking_code`, `payments`.`status` AS `status` FROM `payments` WHERE `payments`.`billno` = '" + billno + "'");

            while (rs.next()) {
                payment = new Payment(rs.getInt("id"), rs.getString("billno"), rs.getString("booking_code"), rs.getInt("status"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ConfigModel.class.getName()).log(Level.SEVERE, null, ex);
        }

        return payment;
    }
}
